package net.confinanz.security;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;


public class RSA {
 
    public static byte[] encrypt(byte[] d, PublicKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        
        return cipher.doFinal(d);
    }
 
 
    public static byte[] decrypt(byte[] d, PrivateKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);

        return cipher.doFinal(d);
    }

    public static KeyPair generateKeys() throws NoSuchAlgorithmException {
    	return KeyPairGenerator.getInstance("RSA").generateKeyPair();
    }
}